package com.jialiang.blossom.uitl;


import com.jialiang.blossom.model.Gua;
import com.jialiang.blossom.consts.Gua_64;
import com.jialiang.blossom.consts.Gua_8;
import com.jialiang.blossom.model.Time;

import java.util.Scanner;

import static com.jialiang.blossom.consts.Gua_64.getEnumByValue;
import static com.jialiang.blossom.uitl.GetNongLiTime.getNongLiIng;


public class PlumBlossom {

    /**
     * 存放 年 月 日 时
     */
    private static int year, month, day, hour;

    // 手动输入
    public static void manualInput() {
        Time time = getNongLiIng();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年");
        year = sc.nextInt();
        System.out.println("请输入月");
        month = sc.nextInt();
        System.out.println("请输入日");
        day = sc.nextInt();
        System.out.println("请输入时");
        hour = sc.nextInt();

        // 处理输入的年份和时辰
        year = getYear(year);
        hour = getChronology(hour);
        // 处理输入的年份和时辰
        year = getYear(year);
        hour = getChronology(hour);

        time.setYear(year);
        time.setHour(hour);

        Gua gua2 = getGua(time);
        Gua_8 up = gua2.getUp();
        Gua_8 down = gua2.getDown();

        String hexagram = up.getBinary()+down.getBinary();
        Gua_64 gua1 = getEnumByValue(hexagram);
        //周易第59卦_涣卦(风水涣)_巽上坎下_易安居吉祥网
        System.out.println("周易第"+gua1.getCode()+"卦_"+gua1.getName()+"卦("+gua1.getMeaning()+")_"+up.getName()+"上"+down.getName()+"下_");
    }


    // 自动获取当前时间
    public static void autoInput() {
        Time noliIng = getNongLiIng();

        noliIng.setYear(getYear(noliIng.getYear()));
        noliIng.setHour(getChronology(noliIng.getHour()));

        Gua gua = getGua(noliIng);
        Gua_8 up = gua.getUp();
        Gua_8 down = gua.getDown();

        String hexagram = up.getBinary()+down.getBinary();
        Gua_64 gua1 = getEnumByValue(hexagram);
        //周易第59卦_涣卦(风水涣)_巽上坎下_易安居吉祥网
        System.out.println("周易第"+gua1.getCode()+"卦_"+gua1.getName()+"卦("+gua1.getMeaning()+")_"+up.getName()+"上"+down.getName()+"下_");
    }


    /**
     * 梅花起卦算法
     *
     * @param time 时间
     * @return 卦
     */
    private static Gua getGua(Time time) {

        int year = time.getYear();
        int month = time.getMonth();
        int day = time.getDay();
        int hour = time.getHour();

        int up = calculate(year + month + day, 8);//计算上卦
        int down = calculate(year + month + day + hour, 8);// 计算下卦
        int dynamic = calculate(year + month + day + hour, 6); // 计算爻动

        Gua_8 upper = Gua_8.values()[up - 1];
        Gua_8 lower = Gua_8.values()[down - 1];

        return new Gua(upper,lower,dynamic);
    }

    /**
     * 起卦
     *
     * @param x 卦以8除，爻以6除
     * @param y 年月日数总和 ||年月日时数总和
     * @return 卦数 || 动爻
     */
    private static int calculate(int y, int x) {
        return y % x == 0 ? x : y % x;
    }

    /**
     * 将获取的年转换为对应的年数
     *
     * @param year
     * @return
     */
    private static int getYear(int year) {
        year = year % 10 + 1;
        return year;
    }

    /**
     * 将获取的时间转换为时辰
     *
     * @param hour 小时数（24小时制）
     * @return 转换后的时辰数
     * @since 2.0.0
     */
    private static int getChronology(int hour) {
        if (hour < 0 || hour > 23) {
            throw new IllegalArgumentException("输入的小时数应在0到23之间");
        }
        return (hour + 1) % 24 / 2 + 1;
    }

}
